package com.monitor.controller;


import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.monitor.entity.*;
import com.monitor.service.*;
import com.monitor.util.BlankUtil;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-05
 */
@RestController
@RequestMapping("/erpUser")
@Api(description="用户")
public class ErpUserController {

    @Resource
    private ErpUserService erpUserService;
    @Resource
    private ErpCustomerService customerService;
    @Resource
    private ErpRegisterService registerService;
    @Resource
    private  ErpPowersService erpPowersService;
    // 关于职务或者权限的
    @Resource
    private ErpRoleService  ErpRoleService;
    @Resource
    private ErpCustomerRoleService ErpCustomerRoleService;

    @Resource
    private  ErpCustomerService erpCustomerService;


    //用户权限(职务)关联表
    @Resource
    private  CustomerInRoleService customerInRoleService;

    @Resource
    private  ErpUserRoleService erpUserRoleService;


    @RequestMapping("/UpdateUserPassword")
    @ApiOperation("修改用户密码")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "password", value = "密码", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> UpdateUserPassword(Integer userId,String password,Integer custId) {
        Result<Integer>  result=new Result<Integer>();
        try{
            ErpUser user=erpUserService.selectById(userId);
            user.setPassword(password);
            ErpCustomer cust=customerService.selectById(custId);
            cust.setPassword(password);
            customerService.updateById(cust);
            result.setCount(1);
            result.setData(erpUserService.updateById(user)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


    @RequestMapping("/UpdateUserRealName")
    @ApiOperation("修改用户昵称")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "realName", value = "用户昵称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> UpdateUserRealName(Integer userId,String realName) {
        Result<Integer>  result=new Result<Integer>();
        try{
            ErpCustomer erpCustomer = customerService.selectById(userId);
            erpCustomer.setCusNickname(realName);

            result.setCount(1);
            result.setData(customerService.updateById(erpCustomer)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


    @RequestMapping("/inertOrUpdateCustUser")
    @ApiOperation("添加Or修改用户信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "String", paramType = "query")*/
    })
    public @ResponseBody
    Result<ErpUser> inertOrUpdateCustUser(String json) {
        Result<ErpUser>  result=new Result<ErpUser>();
        try{
           ErpUser erpUser = JSON.parseObject(json, ErpUser.class);
            int a=0;
            List<ErpUser> erpUsers = erpUserService.selectList(new EntityWrapper<ErpUser>());
           c: for (int i = 0; i < erpUsers.size(); i++) {
                ErpUser user =  erpUsers.get(i);
                if (erpUser.getRealName().equals(user.getRealName())||erpUser.getPhone().equals(user.getPhone())){
                    a=1;
                    break c;
                }

            }

            if((null==erpUser.getId() ||  0==erpUser.getId())&&a==0){
                erpUserService.insert(erpUser);
                ErpRegister register=new ErpRegister();
                register.setRegData(new Date());
                register.setCustomerCode(erpUser.getId());
                register.setEmail(erpUser.geteMail());
                register.setIdentify(erpUser.getIdentify());
                register.setPassword(erpUser.getPassword());
                register.setQq(erpUser.getQq());
                register.setRealName(erpUser.getRealName());
                register.setTell(erpUser.getPhone());
                register.setWeixin(erpUser.getWx());
                registerService.insert(register);
            }else{
                erpUserService.updateById(erpUser);
            }
            result.setCount(1);
            result.setData(erpUser);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }
  @RequestMapping("/addOrUpdateCustUser")
    @ApiOperation("添加Or修改用户信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "String", paramType = "query")*/
    })
    public @ResponseBody
    Result<ErpUser> addOrUpdateCustUser(ErpUser customerCar) {
        Result<ErpUser>  result=new Result<ErpUser>();
        try{

                erpUserService.insertOrUpdate(customerCar);


            result.setCount(1);
            result.setData(customerCar);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getcustUserByPro")
    @ApiOperation("登陆TEST")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "phone", value = "用户手机", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = false, dataType = "String", paramType = "query")
    })
    public @ResponseBody
    Result<Map<String,Object>> getcustUserByPro(String phone, String password) {
        Result<Map<String,Object>> result=new Result<Map<String,Object>>();
        Map<String,Object> map=new HashMap<String,Object>();
        try{
            ErpUser customerCar=erpUserService.selectOne(new EntityWrapper<ErpUser>().eq("password",password).eq("phone",phone));
            //int code=Integer.parseInt(phone);
           ErpCustomer erpCustomer = customerService.selectOne(new EntityWrapper<ErpCustomer>().eq("id",customerCar.getCustomerId()).eq("state",0));
            if(null==customerCar&&null==erpCustomer){
                result.setCount(0);
                result.setData(null);
                result.setCode(1);
                result.setMsg("用户名或者密码错误");
            }else if (null==erpCustomer){
                ErpCustomer erpCustomer1 = customerService.selectById(customerCar.getCustomerId());
                map.put("customer",erpCustomer1);

                //456
                result.setCode(0);
            }else{
                 ErpUser erpUser = erpUserService.selectOne(new EntityWrapper<ErpUser>().eq("customer_id", erpCustomer.getId()));
                map.put("user",erpUser);
                map.put("role",ErpCustomerRoleService.selectById(erpUser.getRolesId()));
                map.put("customer",erpCustomer);
                result.setCode(0);
            }


            result.setCount(1);
            result.setData(map);
        }catch (Exception e){e.printStackTrace();

            result.setCode(1);
        }
        return result;
    }



    @RequestMapping("/getcustUserById")
    @ApiOperation("根据用户id获取用户信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<ErpCustomer> getcustUserById(Integer custId) {
        Result<ErpCustomer> result=new Result<ErpCustomer>();
        try{
            ErpCustomer customerCar=customerService.selectOne(new EntityWrapper<ErpCustomer>().eq("id",custId));

//            ErpCustomer erpCustomer = customerService.selectById(customerCar.getCustomerId());
//            customerCar.setCustomerName(erpCustomer.getCusNickname());
            result.setCount(1);
            result.setData(customerCar);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/removeCustUser")
    @ApiOperation("删除用户信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Integer> removeCustUser(Integer customerCarId) {
        Result<Integer>  result=new Result<Integer>();
        try{

            result.setCount(1);
            result.setData(erpUserService.deleteById(customerCarId)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


    @RequestMapping("/getUserBycustIdPage")
    @ApiOperation("根据客户ID查询用户分页")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpUser>> getUserBycustIdPage(Integer custId,@RequestParam(value="startDate",defaultValue="") String startDate,@RequestParam(value="endDate",defaultValue="") String endDate) {
        Result<List<ErpUser>> result=new Result<>();
        try{
//            Page<ErpUser> page1=new Page<ErpUser>(page,limit);//.between("change_date",startDate,endDate)
            List<ErpUser> carPage=erpUserService.selectList(new EntityWrapper<ErpUser>().eq("customer_id",custId).between("change_date",startDate,endDate));//.ge("change_date",startDate).and().le("change_date",endDate)
            //Page<ErpCustomerCar> page=new Page<ErpCustomerCar>(1,2);
            result.setCount(carPage.size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getcustUserByCustId")
    @ApiOperation("根据客户id获取用户信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpUser>> inertOrUpdateCustomerCar(Integer custId) {
        Result<List<ErpUser>> result=new Result<List<ErpUser>>();
        try{
            List<ErpUser> customerCar=erpUserService.selectList(new EntityWrapper<ErpUser>().eq("customer_id",custId));
            result.setCount(1);
            result.setData(customerCar);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }
    
    @RequestMapping("/getcustUserByUserId")
    @ApiOperation("根据客户id获取用户信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "userId", value = "用户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Map<String, Object>> getcustUserByUserId(Integer userId) {
        Result<Map<String, Object>> result=new Result<Map<String, Object>>();
        try{
        	Map<String, Object> map=new HashMap<>();
            ErpUser user=erpUserService.selectById(userId);
            map.put("user", user);
            String[] groupId=user.getGroupId().split(",");
            List<ErpCustomer> list=new ArrayList<>();
            for(String group:groupId) {
            	int id=Integer.parseInt(group.trim());
            	ErpCustomer cus=customerService.selectById(id);
            	list.add(cus);
            }
            map.put("cusList", list);
            result.setCount(1);
            result.setData(map);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }
    
    @RequestMapping("/updateStutes")
    @ApiOperation("停用启用用户")
    public  Result<Integer> updateStutes(int id,int states){
    	return erpUserService.updateStutes(id, states);
    }

    @RequestMapping("/getRefistercust")
    @ApiOperation("注册列表查询")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "code", value = "客户编码(非必填)", required = false, dataType = "String", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpRegister>> getRefistercust(String code) {
        Result<List<ErpRegister>> result=new Result<List<ErpRegister>>();
        try{
            List<ErpRegister> customerCar=new ArrayList<>();
            if(StringUtils.isEmpty(code)){
                customerCar=registerService.selectList(new EntityWrapper<ErpRegister>());
            }else{
                List<ErpCustomer> customers=customerService.selectList(new EntityWrapper<ErpCustomer>().like("cus_code",code).or().like("cus_name",code));
                List<Integer> custIds=customers.stream().map(ErpCustomer::getId).collect(Collectors.toList());
                String custidsStr= BlankUtil.listToString(custIds);
                List<ErpUser> users=erpUserService.selectList(new EntityWrapper<ErpUser>().in("customer_id",custidsStr));
                List<Integer> userIds=users.stream().map(ErpUser::getId).collect(Collectors.toList());
                String userIdStr=BlankUtil.listToString(userIds);
                customerCar=registerService.selectList(new EntityWrapper<ErpRegister>().in("customer_code",userIdStr));
            }
            result.setCount(1);
            result.setData(customerCar);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/userRegister")
    @ApiOperation("注册列表查询//好像是错的")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
//            @ApiImplicitParam(name = "code", value = "客户编码(非必填)", required = false, dataType = "String", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> userRegister(ErpUser user) {
        Result<Integer> result=new Result<Integer>();
        int phoneIs=0;

        try{
            List<ErpUser> erpUsers = erpUserService.selectList(new EntityWrapper<ErpUser>());
            for (int i = 0; i < erpUsers.size(); i++) {
                ErpUser erpUser =  erpUsers.get(i);
                if (erpUser.getPhone()!=null&&user.getPhone().equals(erpUser.getPhone())){
                    phoneIs=1;
                    break;
                }
            }
            if (user!=null&&phoneIs==0){
                ErpCustomer erpCustomer =new ErpCustomer();
                ErpCustomer erpCustomer1 = erpCustomerService.selectList(new EntityWrapper<>()).get(erpCustomerService.selectList(new EntityWrapper<>()).size() - 1
                );
                erpCustomer.setCusCode(erpCustomer1.getCusCode()+1);
                Date date= new Date();
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
                String format = simpleDateFormat.format(date);
                erpCustomer.setCusZcDate(format);
                erpCustomer.setPassword(user.getPassword());
                erpCustomerService.insert(erpCustomer);

                user.setCustomerId( erpCustomer.getId());
            boolean insert = erpUserService.insert(user);
//            boolean b = customerService.insertOrUpdate(user.getErpCustomer());
                result.setData(erpCustomer1.getCusCode()+1);

            }


        }catch (Exception e){
            e.printStackTrace();
            result.setData(0);
        }

        return  result;

    }


    //erp_role   erp_customer_role  表的增删改查
    @RequestMapping("/updatePost")
    @ApiOperation("对职务管理进行编辑或添加(erp_role)")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "code", value = "客户编码(非必填)", required = false, dataType = "String", paramType = "query")
    })
    public @ResponseBody
    Result<Boolean> updatePost( ErpRole ErpRole) {
        Result<Boolean>  result =new Result<Boolean>();
        try{

                boolean b = ErpRoleService.insertOrUpdate(ErpRole);
                result.setMsg("修改或者添加职务erp_role管理成功");
                result.setData(b);

    }catch (Exception e){e.printStackTrace();}
        return  result;

    }
    @RequestMapping("/updatePostCustomer")
    @ApiOperation("对职务管理进行编辑或添加(erp_customer_role)")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "code", value = "客户编码(非必填)", required = false, dataType = "String", paramType = "query")
    })
    public @ResponseBody
    Result<Boolean> updatePostCustomer( ErpCustomerRole erpCustomerRole){
        Result<Boolean>  result =new Result<Boolean>();
        try{

                boolean b = ErpCustomerRoleService.insertOrUpdate(erpCustomerRole);
                result.setMsg("修改或者添加职务erp_customer_role管理成功");
                result.setData(b);
    }catch (Exception e){e.printStackTrace();}
        return  result;

    }

    @RequestMapping("/selectPostPage")
    @ApiOperation("对职务管理进行查询分页(erp_role)")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "code", value = "客户编码(非必填)", required = false, dataType = "String", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpRole>> selectPostPage( ) {
        Result<List<ErpRole>>  result =new  Result<List<ErpRole>>();
        try{

//            Page<ErpRole> page1=new Page<ErpRole>(page,limit);
            List<ErpRole> erpRoles = ErpRoleService.selectList(new EntityWrapper<ErpRole>());
            for (int i = 0; i < erpRoles.size(); i++) {
                ErpRole erpRole =  erpRoles.get(i);
                List<ErpPowers> id = erpPowersService.selectList(new EntityWrapper<ErpPowers>().in("id", erpRole.getRoleAction()));
                erpRole.setErpPowersList(id);
            }
            result.setMsg("对职务管理进行查询分页(erp_role)");
            result.setData(erpRoles);
        }catch (Exception e){e.printStackTrace();}
        return  result;

    }
    @RequestMapping("/selectPostCustomerPage")
    @ApiOperation("对职务管理进行查询所有((erp_customer_role))")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "code", value = "客户编码(非必填)", required = false, dataType = "String", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpCustomerRole>> selectPostCustomerPage() {
        Result<List<ErpCustomerRole>>  result =new  Result<List<ErpCustomerRole>>();
        try{
//            Page<ErpCustomerRole> page1=new Page<ErpCustomerRole>(page,limit);
//            Page<ErpCustomerRole> erpCustomerRolePage = ErpCustomerRoleService.selectPage(page1);
            List<ErpCustomerRole> erpCustomerRoles = ErpCustomerRoleService.selectList(new EntityWrapper<ErpCustomerRole>());
            for (int i = 0; i < erpCustomerRoles.size(); i++) {
                ErpCustomerRole erpCustomerRole =  erpCustomerRoles.get(i);
                List<ErpPowers> id = erpPowersService.selectList(new EntityWrapper<ErpPowers>().in("id", erpCustomerRole.getRoleAction()));
                erpCustomerRole.setErpPowersList(id);
            }
            result.setMsg("对职务管理进行查询分页((erp_customer_role))");
            result.setData(erpCustomerRoles);
        }catch (Exception e){e.printStackTrace();}
        return  result;

    }
    @RequestMapping("/deletePostPage")
    @ApiOperation("对职务管理进行删除(erp_role)")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "code", value = "客户编码(非必填)", required = false, dataType = "String", paramType = "query")
    })
    public @ResponseBody
    Result<Boolean> deletePostPage(Integer id) {
        Result<Boolean>  result =new  Result<Boolean>();
        try{
            boolean b = ErpRoleService.deleteById(id);
            result.setMsg("对职务管理进行删除(erp_role)");
            result.setData(b);
        }catch (Exception e){e.printStackTrace();}
        return  result;

    }
    @RequestMapping("/deletePostCustomerPage")
    @ApiOperation("对职务管理进行删除((erp_customer_role))")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "code", value = "客户编码(非必填)", required = false, dataType = "String", paramType = "query")
    })
    public @ResponseBody
    Result<Boolean> deletePostCustomerPage( Integer id) {
        Result<Boolean>  result =new  Result<Boolean>();
        try{
            boolean b = ErpCustomerRoleService.deleteById(id);
            result.setMsg("对职务管理进行删除((erp_customer_role))");
            result.setData(b);
        }catch (Exception e){e.printStackTrace();}
        return  result;
    }
    @RequestMapping("/updateUserPasswordRole")
    @ApiOperation("对用户进行修改密码和修改权限")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "code", value = "客户编码(非必填)", required = false, dataType = "String", paramType = "query")
    })
    public @ResponseBody
    Result<Boolean> updateUserPasswordRole( ErpUser erpUser,Integer roleId) {
            Result<Boolean> result=null;
        try{
            boolean b = erpUserService.updateById(erpUser);
            ErpUserRole erpUserRole =new ErpUserRole();
            erpUserRole.setUser_id(erpUser.getId());
            ErpUserRole erpUserRole1 = erpUserRoleService.selectById(erpUserRole);
            erpUserRole1.setRole_id(roleId);
            erpUserRoleService.updateById(erpUserRole1);
            result.setData(b);
            result.setMsg("修改用户密码和权限成功");
        }catch (Exception e){e.printStackTrace();}
        return  result;
    }
    @RequestMapping("/updateCustomerPasswordRole")
    @ApiOperation("对客户进行修改密码和修改权限")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "code", value = "客户编码(非必填)", required = false, dataType = "String", paramType = "query")
    })
    public @ResponseBody
    Result<Boolean> updateCustomerPasswordRole( ErpCustomer erpCustomer,Integer roleCustomerId) {
            Result<Boolean> result=null;
        try{
            boolean b = customerService.updateById(erpCustomer);
            CustomerInRole customerInRole=new CustomerInRole();
            customerInRole.setCustomerId(erpCustomer.getId());
            CustomerInRole customerInRole1 = customerInRoleService.selectById(customerInRole);
            customerInRole1.setCustomerId(roleCustomerId);
            customerInRoleService.updateById(customerInRole1);
            result.setData(b);
            result.setMsg("修改客户密码和权限成功");
        }catch (Exception e){e.printStackTrace();}
        return  result;
    }

    @RequestMapping("/selectPowers")
    @ApiOperation("查询所有权限")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "code", value = "客户编码(非必填)", required = false, dataType = "String", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpPowers>> selectPowers() {
        Result<List<ErpPowers>>  result=new Result<List<ErpPowers>>();
        try{
            List<ErpPowers> erpPowers = erpPowersService.selectList(new EntityWrapper<ErpPowers>());
            for (int i = 0; i < erpPowers.size(); i++) {
                ErpPowers powers =  erpPowers.get(i);
                if (powers.getPid()==0){
                    powers.setErpPowersList(erpPowersService.selectList(new EntityWrapper<ErpPowers>().eq("pid",powers.getId())));
                }
            }
            result.setData(erpPowers);
            result.setMsg("查看所有权限成功");
        }catch (Exception e){e.printStackTrace();}
        return  result;
    }
    @RequestMapping("/selectPowersOne")
    @ApiOperation("查询单个")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "code", value = "客户编码(非必填)", required = false, dataType = "String", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpPowers>> selectPowersOne(String name) {
        Result<List<ErpPowers>>  result=new Result<List<ErpPowers>>();
        try{
            ErpPowers name1 = erpPowersService.selectOne(new EntityWrapper<ErpPowers>().eq("name", name));
            List<ErpPowers> erpPowers = erpPowersService.selectList(new EntityWrapper<ErpPowers>().eq("pid",name1.getId()));

            result.setData(erpPowers);
            result.setMsg("查看权限成功");
        }catch (Exception e){e.printStackTrace();}
        return  result;
    }
    
    @RequestMapping("/selectUserByCode")
    @ApiOperation("根据客户编码查询用户")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "code", value = "客户编码(非必填)", required = false, dataType = "String", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpCustomer>>  selectPowersOne(Integer  code) {
     Result<List<ErpCustomer>>   result=new   Result<List<ErpCustomer>>();
     try{
            List<ErpCustomer> erpCustomerList = customerService.selectList(new EntityWrapper<ErpCustomer>().eq("cus_service_com", code));

            result.setData(erpCustomerList);
            result.setMsg("查询所有用户成功");
        }catch (Exception e){e.printStackTrace();}
        return  result;
    }

}

